#污染日历图
library(tidyverse)
library(lubridate)

conn <- src_sqlite("~/Documents/rockontrol/合肥项目数据/合肥市项目.db")
town <- "滨湖新区"

begin <- "2019-08-01"
end <- "2019-08-31"

begin_month = month(begin)
end_month = month(end)

dataset <- tbl(conn,"日历") %>% collect()
AQIcolor <-tbl(conn,"AQI颜色") %>% collect()
AQI <- tbl(conn,"日数据") %>% filter(日期 >=begin,日期 <= end,站点名称 == town)  %>% collect()

labels <- tibble(首要污染物 = c("O3","PM2.5","PM10","SO2","CO","无","NO2",NA),
                      标记 = c("O[3]","PM[2.5]","PM[10]","SO[2]","CO","无","NO[2]","无"))

AQI %>% 
  select(日期,AQI,首要污染物,等级) %>%
  left_join(AQIcolor ,by = c("等级"="类别")) %>%
  inner_join(dataset,by = "日期") %>%left_join(labels,by = "首要污染物")%>%mutate(日期 = ymd(日期)) %>% 
  ggplot(aes(x = reorder(星期,周),y = -1*行)) + 
  geom_tile(aes(fill = RGB颜色)) +
  scale_fill_identity(guide = "legend",breaks = AQIcolor$RGB颜色,labels= AQIcolor$类别)+
 # scale_x_discrete(position = "top")+
  geom_text (aes(label = 标记),parse = TRUE,size = 8) + 
  facet_wrap(reorder(月份,日期) ~.,scales = "free")+ 
  labs(x="",y="")+ 
  theme_bw()+ 
  theme(axis.text.y = element_blank(),
        axis.ticks  = element_blank(),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom",
        plot.title =   element_text(hjust = 0.5),
        axis.text.x=element_text(size = 15,vjust = 0.5),
        axis.title.y = element_text(size = 15),
        strip.text = element_text(size= 15),
        legend.text.align = 0,
        legend.text = element_text(size = 15)
              )



DBI::dbDisconnect(conn$con)
